//
//  IGCommunityListTableViewCell.m
//  jingdongfang
//
//  Created by BOE on 2017/8/21.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "IGCommunityListTableViewCell.h"
#import "IGCommunityModel.h"

@interface IGCommunityListTableViewCell()

@property (nonatomic, assign) IGCommunityListType listType;

@property (nonatomic, strong) UIImageView *imgView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *detailLabel;

@property (nonatomic, strong) UIButton *unFollowBtn;
@property (nonatomic, strong) UIImageView *rightimgView;

@end

static NSString *const kIGTopicCommentTableViewCellID = @"kIGTopicCommentTableViewCellID";
@implementation IGCommunityListTableViewCell

+ (IGCommunityListTableViewCell *)cellWithTableView:(UITableView *)tableView listType:(IGCommunityListType)listType {
    IGCommunityListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kIGTopicCommentTableViewCellID];
    if (!cell) {
        cell = [[IGCommunityListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kIGTopicCommentTableViewCellID listType:listType];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier listType:(IGCommunityListType)listType {
    self.listType = listType;
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self createUI];
    }
    return self;
}

- (void)createUI {
    CGFloat imgWidth = 60;///图片size
    
    self.imgView = [UIImageView new];
    self.imgView.backgroundColor = [UIColor mainBackgroundColor];
    self.imgView.layer.masksToBounds = YES;
    self.imgView.layer.cornerRadius = 30;
    
    [self.contentView addSubview:self.imgView];
    
    self.titleLabel = [UILabel new];
    self.titleLabel.textAlignment = NSTextAlignmentLeft;
    self.titleLabel.font = [UIFont font18];
    self.titleLabel.textColor = [UIColor c4Color];
    [self.contentView addSubview:self.titleLabel];
    
    self.detailLabel = [UILabel new];
    self.detailLabel.textAlignment = NSTextAlignmentLeft;
    self.detailLabel.font = [UIFont font16];
    self.detailLabel.textColor = [UIColor c5Color];
    [self.contentView addSubview:self.detailLabel];

    self.unFollowBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.unFollowBtn.titleLabel.font = [UIFont font16];
    [self.unFollowBtn setTitle:NSLocalizedString(@"Follow", nil) forState:UIControlStateNormal];
    [self.unFollowBtn setTitle:NSLocalizedString(@"Followed", nil) forState:UIControlStateSelected];
    self.unFollowBtn.layer.borderWidth = 1.0;
    self.unFollowBtn.layer.borderColor = [UIColor c5Color].CGColor;
    self.unFollowBtn.layer.cornerRadius = 15;
    [self.unFollowBtn setTitleColor:[UIColor c4Color] forState:UIControlStateNormal];
    [self.unFollowBtn setTitleColor:[UIColor c5Color] forState:UIControlStateSelected];
    __weak typeof(self) weakSelf = self;
    [self.unFollowBtn setTapActionWithBlock:^{
        if (weakSelf.focusCircle) {
            weakSelf.focusCircle();
        }
    }];
    
    self.rightimgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pub_circle_selected"]];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor mainBackgroundColor];
    [self.contentView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.height.mas_equalTo(1);
    }];
    
    [self.imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(kGAP15);
        make.left.mas_equalTo(kGAP15);
        make.width.height.mas_equalTo(imgWidth);
    }];
    
    CGFloat width = kGAP15;
    if (self.listType == IGCommunityListTypeMeFollow) {
        [self.contentView addSubview:self.unFollowBtn];
        [self.unFollowBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.imgView);
            make.right.mas_equalTo(-1 * kGAP15);
            make.height.mas_equalTo(30);
            make.width.mas_equalTo(75);
        }];
        width += (75+kGAP10);
    } else if (self.listType == IGCommunityListType4TopicPub) {
        [self.contentView addSubview:self.rightimgView];
        [self.rightimgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.imgView);
            make.right.mas_equalTo(-1 * kGAP15);
            make.height.mas_equalTo(24);
            make.width.mas_equalTo(24);
        }];
        width += (24+kGAP10);
    }
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(23);
        make.left.mas_equalTo(self.imgView.mas_right).offset(kGAP15);
        make.height.mas_equalTo(18);
        make.right.mas_equalTo(-width);
    }];
    
    [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(kGAP10);
        make.left.mas_equalTo(self.titleLabel);
        make.height.mas_equalTo(16);
        make.right.mas_equalTo(-width);
    }];
}

- (void)setItemModel:(IGCommunityModel *)itemModel {
    _itemModel = itemModel;
    
    [self.imgView sd_setImageWithURL:[NSURL URLWithString:itemModel.cImgUrl]];
    
    self.titleLabel.text = itemModel.cTitle;
    self.detailLabel.text = itemModel.cDetail;
    self.unFollowBtn.selected = [itemModel.isFocus boolValue];
    self.rightimgView.hidden = !itemModel.isSelected;
}

- (void)setModel:(HomeCommunityModel *)model
{
    _model = model;
    [self.imgView sd_setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:[UIImage imageFromColor:[UIColor mainRandomColor]]];
    self.titleLabel.text = model.title;
    self.detailLabel.text = model.content;
    self.unFollowBtn.selected = [model.isFocus boolValue];
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
